<template>
	<div class="system-post-container">
		<el-card shadow="hover">
			<div class="system-user-search mb15">
				<el-form :inline="true">
					<el-form-item>
						<el-button v-if="permissionStore.isPermissionsBtn('001005001')" size="default" type="success" class="ml10" @click="onOpenAddPost">
							<el-icon>
								<FolderAdd />
							</el-icon>
							新增岗位
						</el-button>
					</el-form-item>
				</el-form>
			</div>
			<el-table :data="tableData.data" style="width: 100%" >
				<el-table-column type="index" label="序号" width="60" align="center" />
				<el-table-column prop="postName" label="岗位名称" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column prop="postSort" label="岗位名称" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="岗位状态" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status === 'ENABLE'">正常</el-tag>
						<el-tag type="info" v-else>停用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="remark" label="岗位描述" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createBy" label="创建人" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createTimeTxt" label="创建时间" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column prop="updateBy" label="更新人员" show-overflow-tooltip></el-table-column>
				<el-table-column prop="updateTimeTxt" label="更新时间" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="200" v-if="permissionStore.isPermissionsBtn(['001005002','001005003'])">
					<template #default="scope">
						<el-button v-if="permissionStore.isPermissionsBtn('001005002')" size="small" text type="warning" @click="onOpenEditPost(scope.row)">修改</el-button>
						<el-button v-if="permissionStore.isPermissionsBtn('001005003')" size="small" text type="danger" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNumber"
				v-model:limit="tableData.param.pageLength" @pagination="postList" />
		</el-card>
		<EditPost ref="editPostRef" @getPostList="postList" />
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, ref, defineComponent, toRaw } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EditPost from './component/editPost.vue';
import { getPostListApi, delPostApi } from "@/api/auth/post";
import { formatDateTime } from '@/utils'
import {usePermissionStore} from '@/store/modules/permission'
// 定义接口来定义对象的类型
interface TableData {
	postId: number;
	postCode: string;
	postName: string;
	postSort: number;
	status: number;
	remark: string;
	createdAt: string;
}
interface TableDataState {
	ids: number[];
	tableData: {
		data: Array<TableData>;
		total: number;
		loading: boolean;
		param: {
			pageNumber: number;
			pageLength: number;
		};
	};
}

export default defineComponent({
	name: 'apiV1SystemPostList',
	components: { EditPost },
	setup() {
		const addPostRef = ref();
		const editPostRef = ref();
		  const permissionStore = usePermissionStore()
		const state = reactive<TableDataState>({
			ids: [],
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					pageNumber: 1,
					pageLength: 10,
				},
			},
		});
		// 初始化表格数据
		const initTableData = () => {
			postList()
		};
		const postList = () => {
			getPostListApi(state.tableData.param).then((res: any) => {
				state.tableData.data = res.data.list.map(item => {
					let obj = {
						updateTimeTxt: formatDateTime(item.updateTime),
						createTimeTxt: formatDateTime(item.createTime),
						...item
					}
					return obj
				})
				state.tableData.total = res.data.total;
			})
		};
		// 打开新增岗位弹窗
		const onOpenAddPost = () => {
			editPostRef.value.openDialog();
		};
		// 打开修改岗位弹窗
		const onOpenEditPost = (row: Object) => {
			editPostRef.value.openDialog(toRaw(row));
		};
		// 删除岗位
		const onRowDel = (row: any) => {
			let msg = `此操作将永久删除岗位：“${row.postName}”，是否继续?`
			ElMessageBox.confirm(msg, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					delPostApi({ id: row.postId }).then(() => {
						ElMessage.success('删除成功');
						postList();
					})
				})
				.catch(() => { });
		};

		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		// 多选框选中数据
		const handleSelectionChange = (selection: Array<TableData>) => {
			state.ids = selection.map(item => item.postId)
		};
		return {
			addPostRef,
			editPostRef,
			permissionStore,
			onOpenAddPost,
			onOpenEditPost,
			onRowDel,
			postList,
			handleSelectionChange,
			...toRefs(state),
		};
	},
});
</script>
